<?php


namespace app\admin\model;


use app\Exception\UserException;
use think\Exception;

class UserModel extends BaseAdminModel
{
    protected $table = "blog_admin_user";

    /**
     * 校验用户名密码
     * @param $email 邮箱
     * @param $pwd 密码
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function checkUserInfo(string $email, string $pwd): array
    {
        $user = $this->where('email', '=', $email)
            ->find();
        if ($user == null) {
            throw new UserException('40002', '账号不存在');
        }
        $arr = $user->toArray();
        if ($arr['password'] != $pwd) {
            throw new UserException('40001', '账号或者密码错误');
        }
        return $user->toArray();
    }

    public function updateToken(string $email, string $token, string $refresh_token, string $ip)
    {
        $this->where('email', '=', $email)
            ->save([
                'token' => $token,
                'refresh_token' => $refresh_token,
                'last_ip' => $ip
            ]);
    }
}